package com.bosssoft.service.impl;

import com.bosssoft.entity.dto.FibRoleDTO;
import com.bosssoft.enums.CopyPropertityEnum;
import com.bosssoft.exception.DaoException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bosssoft.mapper.FibRoleMapper;
import com.bosssoft.entity.po.FibRolePO;
import com.bosssoft.service.FibRoleService;
/**
 * @author Li kexi
 * Created on 2021/8/16.14:32
 */
@Slf4j
@Service
public class FibRoleServiceImpl extends ServiceImpl<FibRoleMapper, FibRolePO> implements FibRoleService{

    @Override
    public boolean addRole(FibRoleDTO roleDTO) {
        try {
            FibRolePO rolePO = new FibRolePO();
            BeanUtils.copyProperties(roleDTO, rolePO);
            return this.baseMapper.insert(rolePO) > 0;
        } catch (Exception e) {
            //TODO 换异常处理
        }
        return false;
    }

    @Override
    public boolean modifyRole(FibRoleDTO roleDTO) {
        try {
            FibRolePO rolePO = new FibRolePO();
            BeanUtils.copyProperties(roleDTO, rolePO);
            return this.baseMapper.updateById(rolePO) > 0;
        } catch (Exception e) {
            //TODO 换异常处理
        }
        return false;
    }

    @Override
    public boolean banRoleById(Long roleId, Integer status) {
        return this.baseMapper.banRoleById(roleId, status) > 0;
    }

    @Override
    public boolean deleteRoleById(Long roleId) {
        return this.baseMapper.deleteById(roleId) > 0;
    }

    @Override
    public String getRoleNameById(Long id) {
        try {
            FibRolePO rolePO = this.baseMapper.selectById(id);
            return rolePO.getRoleName();
        } catch (Exception e) {
            log.info("getRoleNameById出错!");
            throw DaoException.error();
        }
    }

    /**
     * 以下是自动生成的代码。基本不用。
     */

    @Override
    public int updateBatch(List<FibRolePO> list) {
        return baseMapper.updateBatch(list);
    }
    @Override
    public int updateBatchSelective(List<FibRolePO> list) {
        return baseMapper.updateBatchSelective(list);
    }
    @Override
    public int batchInsert(List<FibRolePO> list) {
        return baseMapper.batchInsert(list);
    }
    @Override
    public int insertOrUpdate(FibRolePO record) {
        return baseMapper.insertOrUpdate(record);
    }
    @Override
    public int insertOrUpdateSelective(FibRolePO record) {
        return baseMapper.insertOrUpdateSelective(record);
    }

    /**
     * 以下是测试用代码
     */

    @Override
    public List<FibRoleDTO> getAllRoles() {
        try {
            List<FibRolePO> allRoles = this.baseMapper.getAllRoles();
            log.info("allRoles:{}", allRoles);
            List<FibRoleDTO> rolesDTOList = new ArrayList<>(allRoles.size());
            for (int i = 0; i<allRoles.size(); ++i) {
                rolesDTOList.add(new FibRoleDTO());
                BeanUtils.copyProperties(allRoles.get(i), rolesDTOList.get(i));
                log.info("rolesDTOList:{}", rolesDTOList.get(i));
            }
            return rolesDTOList;
        } catch (Exception e) {
            log.info("出问题了");
            throw DaoException.error().setCodeAndMessage(CopyPropertityEnum.RESOURCE_NULL_ERROR);
        }
    }
}
